package com.lcw.lostandfound.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcw.lostandfound.mapper.GoodsMapper;
import com.lcw.lostandfound.pojo.Goods;
import com.lcw.lostandfound.pojo.User;
import com.lcw.lostandfound.service.GoodsService;
import com.lcw.lostandfound.util.RandomCodeUtils;
import com.lcw.lostandfound.util.UploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class ReleaseController {

    @Autowired
    private UploadUtils uploadUtils;
    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/randomcode")
    public void createCode(HttpServletRequest req, HttpServletResponse resp){
        RandomCodeUtils.createRandomCode(req,resp);
    }

    //我的发布
    @RequestMapping("/myRelease")
    public String myRelease(HttpSession session,Model model){
        User user = (User) session.getAttribute("user");
        model.addAttribute("msgs",goodsService.findByUserId(user.getId()));
        return "MyRelease";
    }
    @RequestMapping("/releaseInformation")
    public String releaseInformation(@RequestParam(name = "key") String key, HttpSession session, Model model){
        if (session.getAttribute("user")==null){
            return "login";
        }
        if("lostor".equals(key)){
            model.addAttribute("key","lostor");
        }else {
            model.addAttribute("key", "ta");
        }
        return "ReleaseInformation";
    }
    @RequestMapping("/hello")
    public String hello(Integer pageNum,Integer pageSize,Integer type,Model model){
        PageInfo<Goods> info = goodsService.listFindMsg(pageNum, pageSize, type);
        model.addAttribute("info",info);
        return "hello";
    }
    @RequestMapping("/release")
    public String release(HttpSession session,Goods goods){
        User user = (User) session.getAttribute("user");
        if(user!=null) {
            goods.setUserId(user.getId());
        }
        Integer id = goodsService.release(goods);
        return "redirect:/detail?id="+id;
    }
    @ResponseBody
    @RequestMapping(value = "/upload")
    public String upload(String dir, MultipartFile imgFile, HttpSession session){
        String ret = null;
        try {
            ret = uploadUtils.uploadImage(dir, imgFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;

    }
    @ResponseBody
    @RequestMapping(value = "/checkRandom")
    public String checkRandom(String random,HttpSession session){
        if(!random.equalsIgnoreCase(session.getAttribute("RANDOMCODE_IN_SESSION").toString())){
            return "false";
        }
        return "true";
    }
}
